/**
 * 
 */
package com.ekingstar.framework.app;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 
 *
 */
public class CodeImageFilter implements Filter {

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	private Properties props = new Properties();
	private String userName;
	private String userNameSessionKey;
	private String sessionKeyValue ;
	private String exceptionKey ;
	private String failUrl = null;
	private String imageCodeName =null;
	public void destroy() {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest reqeust = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)res;
		HttpSession session =  reqeust.getSession();
		if (session == null) chain.doFilter(req, res);
		else {
			session.setAttribute(userNameSessionKey, req.getParameter(userName));
			String code = (String)session.getAttribute(sessionKeyValue);
			String inputCode = reqeust.getParameter(imageCodeName);
			if (code != null && (inputCode == null || inputCode.trim().equals(""))){
				session.setAttribute(exceptionKey, new NoInputCodeException("没有输入验证码"));
				response.sendRedirect(failUrl);
				return;
			}
			if (code == null){
				chain.doFilter(req, res);
				return;
			}
			if (code.trim().equals(inputCode.trim())){
				chain.doFilter(req, res);
				return;
			}
			
			session.setAttribute(exceptionKey, new ErrorCodeException("验证码不正确"));
			response.sendRedirect(failUrl);
		}

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig conf) throws ServletException {
		Enumeration<?> initParams = conf.getInitParameterNames();
		while (initParams.hasMoreElements())
		{
			String key = (String) initParams.nextElement();
			String value = conf.getInitParameter(key);
			props.put(key, value);
		}
		failUrl = props.getProperty("failUrl", "/");
		exceptionKey = props.getProperty("exceptionKey", "SPRING_SECURITY_LAST_EXCEPTION");
		imageCodeName = props.getProperty("imageCodeName", "imageCodeName");
		userName = props.getProperty("userName");
		userNameSessionKey = props.getProperty("userNameSessionKey");
		sessionKeyValue = props.getProperty("sessionKey", "KAPTCHA_SESSION_KEY");
	}

}
